/**
 * Author: w景洋.
 * Time: 2016/7/6 0006
 */
angular.module('service_class.wpzr', [
        "ngResource",
        "ngStorage"
    ])

    .config(['$stateProvider','$urlRouterProvider',
        function($stateProvider, $urlRouterProvider) {
            $stateProvider

                .state('model.wpzr', {
                    url: '/wpzr',
                    templateUrl: service_templateprefix + "service_class/wpzr.html",
                    controller: 'WpzrController'
                })
                .state('model.restrans',{//餐馆转让信息
                    url : '/restrans',
                    templateUrl : service_templateprefix + "service_class/restrans.html",
                    controller : 'RestransController'
                })
                .state('model.resinfo',{//餐馆信息
                    url : '/resinfo?sId',
                    templateUrl : service_templateprefix + "service_class/resinfo.html",
                    controller : 'ResinfoController'
                })

        }])

    .controller('WpzrController', ['$scope', '$state','CgzrController', function($scope,$state,CgzrController) {
        //$scope.cmtUrl = service_templateprefix;

        $scope.restalist = CgzrController.getCgzrList();
        $scope.restalist.$promise.then(function(list){
            console.log(list)
            $scope.cgzrlist=list;
        });
        //分页
        $scope.currentPage = 1;
        $scope.pageSize = 8;

        /* //获取分页的页数
         $scope.cgzrPagingNum = CgzrService.getPagingNum();
         $scope.cgzrPagingNum.$promise.then(function(data){
         console.log(data.n);

         $scope.cgzrMaxNum = data.n;
         });
         //根据页数获取列表
         var sNum = 1;
         $scope.sNumPlus = function(){
         if(sNum<$scope.cgzrMaxNum){
         sNum++;
         //console.log(sNum);
         $scope.cgzrlist = CgzrService.getPagingList(sNum);
         }else {
         alert("没有下一页了哦")
         }
         }
         $scope.sNumDecrease = function(){
         if(sNum>1){
         sNum--;
         //console.log(sNum);
         $scope.cgzrlist = CgzrService.getPagingList(sNum);
         }else {
         alert("已经是最前一页了")
         }
         }
         //console.log(sNum);
         $scope.cgzrlist = CgzrService.getPagingList(sNum);
         $scope.cgzrlist.$promise.then(function(data){
         //console.log($scope.cgzrlist);
         });*/


        //截取字符串
        $scope.strsplit=function(s){
            if(s==undefined){
                return true;
            }else{
                return s.toString().split("-");
            }
        }

        $scope.min='';
        $scope.max='';
        $scope.name='';
        $scope.getPagingSearchList=function(){

            CgzrController.getPagingSearchList($scope.adr,$scope.name,$scope.min,$scope.max).$promise.then(function(data){
                console.log(data)
                $scope.cgzrlist=[];
                $scope.cgzrlist=data;
            })
        }
        //adr
        $scope.aplitadr=function(adr){
            //console.log(area);
            $scope.getPagingSearchList();
        }

        //area
        $scope.aplitarea=function(area){
            //console.log(area);
            if(area==''){
                $scope.name='';
            }
            else{
                $scope.name='area';
            }
            $scope.rent="";
            $scope.transferFee="";
            var str=$scope.strsplit(area);
            $scope.min=str[0];
            $scope.max=str[1];
            $scope.getPagingSearchList();
        }

        /*$scope.filterArea = function(val) {
         if($scope.areaMin==undefined || $scope.areaMin==''){
         return val;
         }
         return (val.area >=  $scope.areaMin && val.area  <= $scope.areaMmax);
         };*/

        //rent
        $scope.aplitrent=function(rent){
            if(rent==''){
                $scope.name='';
            }
            else{
                $scope.name='rent';
            }
            $scope.area="";
            $scope.transferFee="";
            var str=$scope.strsplit(rent);
            $scope.min=str[0];
            $scope.max=str[1];
            $scope.getPagingSearchList();
        }
        /*$scope.filterRent = function(val) {
         if($scope.rentMin==undefined || $scope.rentMin==''){
         return val;
         }
         return (val.rent >=  $scope.rentMin && val.rent  <= $scope.rentMmax);
         };*/
        //transferFee
        $scope.aplittransferFee=function(transferFee){
            if(transferFee==''){
                $scope.name='';
            }
            else{
                $scope.name='transfer_fee';
            }
            $scope.area="";
            $scope.rent="";
            var str=$scope.strsplit(transferFee);
            $scope.min=str[0];
            $scope.max=str[1];
            $scope.getPagingSearchList();
        }
        /*$scope.filtertransferFee = function(val) {
         if($scope.transferFeeMin==undefined || $scope.transferFeeMin==''){
         return val;
         }
         return (val.transferFee >=  $scope.transferFeeMin && val.transferFee  <= $scope.transferFeeMmax);
         };*/
    }])
    //餐馆转让信息页面
    .controller('RestransController', ['$scope', '$state', 'RestaService','fileUpload',
        function($scope,$state,RestaService,fileUpload) {
            $scope.resta = "";
            $scope.putRestaInfo=function(){
                $scope.success_func = function(){//提交成功回调函数
                    $state.go("wpzr");
                }
                $scope.error_func = function(data){//提交失败回调函数
                    alert(data.data.reason);
                }
                RestaService.putRestaInfo($scope.resta,$scope.success_func,$scope.error_func);
            };
            $scope.uploadFile = function(){

                if($scope.myFile==undefined){
                    alert("请选择图片！");
                    return;
                }
                var file = $scope.myFile;
                fileUpload.uploadFileToUrl(file).then(function(data){
                    $scope.resta.logo = data;
                    if(data!=undefined){
                        return;
                    }
                });
            };

            $scope.uploadFile_photo1 = function(){
                if($scope.myFile_photo1==undefined){
                    alert("请选择图片！");
                    return;
                }
                var file = $scope.myFile_photo1;
                fileUpload.uploadFileToUrl(file).then(function(data){
                    $scope.resta.photo1 = data;
                    if(data!=undefined){
                        return;
                    }
                });
            };
            $scope.uploadFile_photo2 = function(){
                if($scope.myFile_photo2==undefined){
                    alert("请选择图片！");
                    return;
                }
                var file = $scope.myFile_photo2;
                fileUpload.uploadFileToUrl(file).then(function(data){
                    $scope.resta.photo2 = data;
                    if(data!=undefined){
                        return;
                    }
                });
            };
        }])
    //餐馆信息页面
    .controller('ResinfoController', ['$scope', '$state','$stateParams', 'ResinfoService',
        function($scope,$state,$stateParams,ResinfoService) {
            console.log("12")
            $scope.resinfolist = ResinfoService.getResinfoList($stateParams.sId);
            $scope.resinfolist.$promise.then(function(data){
                console.log($scope.resinfolist);
            });
            adaptShow(2000);//2000ms轮播一次,函数在util
            //slid("slider")
        }])




//轮播插件
function adaptShow(time){
    $('#carousel-generic').carousel();
    $(function(){
        $('#carousel-generic').carousel({
            interval: time
        });
    });
}

function  slid(slid) {
    var _SlideshowTransitions = [
        { $Duration: 500, x: -0.3, $During: { $Left: [0.3,0.7] }, $Easing: { $Left: $JssorEasing$.$EaseInCubic, $Opacity: $JssorEasing$.$EaseLinear }, $Opacity: 2 }

    ];
    var options = {
        $AutoPlay: true,
        $AutoPlayInterval: 3000,
        $PauseOnHover: 1,
        $DragOrientation: 1,
        $ArrowKeyNavigation: true,
        $SlideDuration: 500,
        $SlideshowOptions: {
            $Class: $JssorSlideshowRunner$,
            $Transitions: _SlideshowTransitions,
            $TransitionsOrder: 1,
            $ShowLink: true
        },

        $ThumbnailNavigatorOptions: {
            $Class: $JssorThumbnailNavigator$,
            $ChanceToShow: 2,
            $ActionMode: 1,
            $SpacingX: 5,
            $DisplayPieces: 100,
            $ParkingPosition: 360
        }
    };

    var jssor_slider1 = new $JssorSlider$(slid, options);
    function ScaleSlider() {
        var parentWidth = jssor_slider1.$Elmt.parentNode.clientWidth;
        if (parentWidth)
            jssor_slider1.$ScaleWidth(Math.max(Math.min(parentWidth, 800), 300));
        else
            window.setTimeout(ScaleSlider, 300);
    }

    ScaleSlider();
    $(window).bind("load", ScaleSlider);
    $(window).bind("resize", ScaleSlider);
    $(window).bind("orientationchange", ScaleSlider);
};